package com.zmwx.subject.domain.service.impl;

import com.alibaba.fastjson.JSON;
import com.zmwx.subject.common.enums.CategoryTypeEnum;
import com.zmwx.subject.common.enums.IsDeletedFlagEnum;
import com.zmwx.subject.domain.convert.SubjectLabelConverter;
import com.zmwx.subject.domain.entity.SubjectLabelBO;
import com.zmwx.subject.infra.basic.entity.SubjectCategory;
import com.zmwx.subject.infra.basic.entity.SubjectLabel;
import com.zmwx.subject.infra.basic.entity.SubjectMapping;
import com.zmwx.subject.infra.basic.service.SubjectCategoryService;
import com.zmwx.subject.infra.basic.service.SubjectLabelService;
import com.zmwx.subject.infra.basic.service.SubjectMappingService;
import com.zmwx.subject.domain.service.SubjectLabelDomainService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;


@Service
@Slf4j
public class SubjectLabelDomainServiceImpl implements SubjectLabelDomainService {

    @Resource
    SubjectLabelService subjectLabelService;
    @Resource
    private SubjectMappingService subjectMappingService;

    @Resource
    private SubjectCategoryService subjectCategoryService;

    @Override
    public boolean add(SubjectLabelBO subjectLabelBO) {
        SubjectLabel subjectLabel = SubjectLabelConverter.INSTANCE.convertBoToLabel(subjectLabelBO);
        if (log.isInfoEnabled()) {
            log.info("*******subjectLabel:{}", JSON.toJSONString(subjectLabel));
        }
        int count = subjectLabelService.insert(subjectLabel);
        return count > 0;
    }

    @Override
    public boolean update(SubjectLabelBO subjectLabelBO) {
        SubjectLabel subjectLabel = SubjectLabelConverter.INSTANCE.convertBoToLabel(subjectLabelBO);
        if (log.isInfoEnabled()) {
            log.info("*******subjectLabel.update:{}", JSON.toJSONString(subjectLabel));
        }
        int count = subjectLabelService.update(subjectLabel);
        return count > 0;
    }

    @Override
    public boolean delete(SubjectLabelBO subjectLabelBO) {
        SubjectLabel subjectLabel = SubjectLabelConverter.INSTANCE.convertBoToLabel(subjectLabelBO);
        subjectLabel.setIsDeleted(IsDeletedFlagEnum.DELETED.code);
        int count = subjectLabelService.update(subjectLabel);
        return count > 0;
    }

    /**
     * 根据分类查询标签
     *
     * @param subjectLabelBO
     * @return
     */
    @Override
    public List<SubjectLabelBO> queryLabelByCategoryId(SubjectLabelBO subjectLabelBO) {
        //如果当前分类是一级分类 查询所有标签
        SubjectCategory subjectCategory = subjectCategoryService.queryById(Long.valueOf(subjectLabelBO.getCategoryId()));
        if (CategoryTypeEnum.PRIMARY.getCode() == subjectCategory.getCategoryType()) {
            if (log.isInfoEnabled()) {
                log.info("*******一级查询分类*************");
            }
            SubjectLabel subjectLabel = new SubjectLabel();
            subjectLabel.setCategoryId(subjectLabelBO.getCategoryId());
            List<SubjectLabel> subjectLabels = subjectLabelService.queryByCondtion(subjectLabel);
            List<SubjectLabelBO> subjectLabelList = SubjectLabelConverter.INSTANCE.convertLabelToBoList(subjectLabels);
            return subjectLabelList;
        }
        Integer categoryId = subjectLabelBO.getCategoryId();
        SubjectMapping subjectMapping = new SubjectMapping();
        subjectMapping.setCategoryId(categoryId.longValue());
        subjectMapping.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.code);
        log.info("***************subjectMapping.queryLabelByCategoryId:{}", JSON.toJSONString(subjectMapping));
        List<SubjectMapping> subjectMappingList = subjectMappingService.queryLabelId(subjectMapping);
        log.info("***************subjectMappingList:{}", JSON.toJSONString(subjectMappingList));
        if (CollectionUtils.isEmpty(subjectMappingList)) {
            return Collections.emptyList();
        }
        log.info("我看一下这个subjectMappingList:{}", JSON.toJSONString(subjectMappingList));

        List<Long> labelIdList = subjectMappingList.stream().map(SubjectMapping::getLabelId).collect(Collectors.toList());
        log.info("我看一下这个流:{}", JSON.toJSONString(labelIdList));
        List<SubjectLabel> labelList = subjectLabelService.batchQueryById(labelIdList);
        List<SubjectLabelBO> labelBOList = new LinkedList<>();
        labelList.forEach(label -> {
            SubjectLabelBO bo = new SubjectLabelBO();
            bo.setId(label.getId());
            bo.setLabelName(label.getLabelName());
            bo.setCategoryId(label.getCategoryId());
            labelBOList.add(bo);
        });
        return labelBOList;
    }
}
